import 'package:eyepetizer_example/config/color.dart';
import 'package:eyepetizer_example/utils/cache_image.dart';
import 'package:eyepetizer_example/widget/go_navigation.dart';
import 'package:flutter/cupertino.dart';

import '../../model/discovery/news_model.dart';
import '../../state/base_list_state.dart';
import '../../viewmodel/discovery/news_viewmodel.dart';
import '../web_page.dart';

const TEXT_CARD_TYPE = "textCard";
const INFORMATION_CARD_TYPE = "informationCard";

class NewsPage extends StatefulWidget {
  const NewsPage({Key? key}) : super(key: key);

  @override
  State<NewsPage> createState() => _NewsPageState();
}

class _NewsPageState
    extends BaseListState<NewsItemModel, NewsViewModel, NewsPage> {
  @override
  Widget getContentChild(NewsViewModel model) {
    return ListView.builder(
      itemBuilder: (context, index) {
        if (model.itemList[index].type == TEXT_CARD_TYPE) {
          // 资讯时间
          return _itemTitle(model.itemList[index]);
        } else {
          // 资讯内容
          return _itemContent(model.itemList[index]);
        }
      },
      itemCount: model.itemList.length,
    );
  }

  @override
  NewsViewModel get viewModel => NewsViewModel();

  /// 资讯时间
  Widget _itemTitle(NewsItemModel itemList) {
    return Container(
      padding: const EdgeInsets.only(left: 10, top: 5),
      child: Text(
        "${itemList.data?.text}",
        style: const TextStyle(
          fontSize: 22,
          fontWeight: FontWeight.bold,
          color: Color(0xff3f3f3f),
        ),
      ),
    );
  }

  Widget _itemContent(NewsItemModel itemList) {
    return GestureDetector(
      onTap: () {
        var actionUrl = itemList.data?.actionUrl;
        if(actionUrl == null) {
          return;
        }
        String url = Uri.decodeComponent(actionUrl.substring(actionUrl.indexOf("url")));
        url = url.substring(4, url.length);
        GoNavigator.toPage(context, WebPage(url: url,));
      },
      child: Padding(
        padding: const EdgeInsets.all(10),
        child: PhysicalModel(
          color: const Color(0xFFEDEDED),
          clipBehavior: Clip.antiAlias,
          borderRadius: BorderRadius.circular(4),
          child: Padding(
            padding: const EdgeInsets.only(bottom: 10),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Padding(
                  padding: const EdgeInsets.only(bottom: 10),
                  child: cacheImage("${itemList.data?.backgroundImage}",
                      width: MediaQuery.of(context).size.width,
                      height: 140,
                      fit: BoxFit.fill),
                ),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: _getTitleList(itemList.data?.titleList),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }

  List<Widget> _getTitleList(List<String>? titleList) {
    if (titleList == null) {
      return [];
    }
    return titleList
        .map((title) => Padding(
              padding: const EdgeInsets.fromLTRB(10, 5, 10, 5),
              child: Text(
                title,
                style: const TextStyle(
                  color: AppColor.desTextColor,
                  fontSize: 12,
                ),
                maxLines: 3,
                overflow: TextOverflow.ellipsis,
              ),
            ))
        .toList();
  }
}
